﻿using System;
using System.Collections.Generic;
using System.Drawing;

using G.Model;
using GameControls;

namespace GameDesignControls
{
    class BoardShapeDisplayerDT : BoardShapeDisplayer
    {
        public BoardShapeDisplayerDT(
            GameDisplayer displayer, BoardShape shape,
            BoardShapeContainer shapeContainer)
            : base(displayer, shape.Location, shape, shapeContainer)
        {
            this.Shape.MouseClick += Shape_MouseClick;
            displayer.GameDef.GameElementDefs.PropertyChangedEvent += this.OnPropertyChanged;
            displayer.GameDef.GamePlayerDefs.PropertyChangedEvent += OnPropertyChangedEvent;
            DisplayIcon();
        }

        public override void Dispose()
        {
            base.ShapeContainer.Remove(base.Shape);
            this.Shape.MouseClick -= Shape_MouseClick;
            base.GameDisplayer.GameDef.GameElementDefs.PropertyChangedEvent -= this.OnPropertyChanged;
            base.GameDisplayer.GameDef.GamePlayerDefs.PropertyChangedEvent -= OnPropertyChangedEvent;
        }
        
        private void Shape_MouseClick(Object target, EventArgs e)
        {
            if (GameDisplayer.GameDef.GameElementDefs.Contains(Location))
            {
                GameElementDef element = GameDisplayer.GameDef.GameElementDefs[Location];
                //ActiveObject.Object = element;
                base.GameDisplayer.ActiveObject.SelectedObject = element;
            }
            else
            {
                GameSpaceDef game = base.GameDisplayer.GameDef;
                GameElementDef element = new GameElementDef(game, base.Location);
                game.GameElementDefs.Add(element);
                DisplayIcon();
                base.GameDisplayer.ActiveObject.SelectedObject = element;
            }
        }
        
        public override void DisplayIcon()
        {
            GameElementDef e = null;
            if (GameDisplayer.GameDef.GameElementDefs.Contains(Location))
            {
                e = GameDisplayer.GameDef.GameElementDefs[Location];
            }
            
            string icon = GameDisplayer.GameDef.GlobalDef.DefaultIcon;
            if (e != null && e.Icon != "")
            {
                icon = e.Icon;
            }
            if (icon == null)
            {
                icon = "";
            }
            
            Dictionary<string, Image> list = 
                base.GameDisplayer.ShapeContainer.GameUserControl.ImageDictionary;
            if (list != null)
            {
                Image image = null;
                if (icon != "")
                {
                    foreach (string key in list.Keys) 
                    {
                        if (key.ToLower().Contains(icon.ToLower()))
                        {
                            image = list[key];
                        }
                    }
                }
                
                if (e != null && GameDisplayer.GameDef.GamePlayerDefs.Contains(e.Player))
                {
                    GamePlayerDef player = 
                        GameDisplayer.GameDef.GamePlayerDefs[e.Player];
                    Shape.BackColor = player.Color;
                }
                    
                if (image != null)
                {
                    Shape.BackgroundImage = image;
                    Shape.Text = "";
                }
                else
                {
                    if (e != null)
                    {
                        Shape.Text = e.Text;
                    }
                    
                    Shape.BackgroundImage = null;
                }
                
                Shape.Invalidate();
            }
        }

        void OnPropertyChangedEvent(GamePlayerDef player)
        {
            DisplayIcon();
        }

        private void OnPropertyChanged(GameElementDef element)
        {
            if (element.Location == this.Location)
            {
                DisplayIcon();
            }
        }
    }
}
